<template>
  <a-row :class="['p-4', `${prefixCls}--box`]" type="flex" :gutter="10" style="max-height: 800px">
    <a-col :xl="6" :lg="24" :md="24" style="margin-bottom: 10px">
      <DepartLeftTree ref="leftTree" @select="onTreeSelect" />
    </a-col>
    <a-col :xl="18" :lg="24" :md="24" style="margin-bottom: 10px">
      <div style="height: 100%" class="address-book">
        <a-card :bordered="false" class="j-print-demo" title="员额基本核定情况">
          <a-tag color="success">{{ xzgs }}</a-tag>
          <!-- v-show="xzgs=='请选择公司' ? false : true" -->
          <div v-show="xzgs == '请选择公司' ? false : true">
            <a-card :bordered="false" class="j-print-demo" title="公司机构信息" style="margin-top: 2%">
              <div class="ldbz">
                <span style="margin-left: 2%; margin-right: 2%">公司总员额数:</span>
                <a-input-number v-model:value="Leadinggroupnumber" :min="0" :max="1000" />
                <span style="margin-left: 2%;margin-right: 2%">机构数:</span>
                <a-input-number v-model:value="Internalmechanism" :min="0" :max="1000" />
                <span style="margin-left: 2%; margin-right: 2%">分/子公司总数:</span>
                <a-input-number v-model:value="Internalmechanismnumber" :min="0" :max="1000" />
              </div>
            </a-card>
            <a-card :bordered="false" class="j-print-demo" title="部门信息" style="margin-top: 2%">
              <div v-for="(department, key) in departmentInfo" :key="key" class="ldbz" style="margin-top: 2%">
                <div style="margin-right: 2%">{{ department.title }}:</div>
                <div style="margin-right: 2%">职数:</div>
                <a-input-number v-model:value="department.position" :min="0" :max="1000" />
                <!-- <JDictSelectTag v-model:value="department.chiefPositionDeputyPositionStr" placeholder="请选择职数"
                  dictCode="ryzs" style="width: 100px;" /> -->
                <span style="margin-left: 2%; margin-right: 2%">员额数:</span>
                <a-input-number v-model:value="department.member" :min="0" :max="1000" />
              </div>
            </a-card>
            <div style="text-align: right">
              <a-button type="primary" ghost style="margin-top: 2%" @click="saveData">保存</a-button>
            </div>
          </div>
        </a-card>
      </div>
    </a-col>
  </a-row>
</template>

<script lang="ts" setup>
import { JDictSelectTag } from '/@/components/Form';
import { provide, ref } from 'vue';
import { useDesign } from '/@/hooks/web/useDesign';
import DepartLeftTree from './components/DepartLeftTree.vue';
import { queryDepartTreeSync, saveOrUpdateDepart } from './address.api';
import { message } from 'ant-design-vue';
const { prefixCls } = useDesign('address-list');
provide('prefixCls', prefixCls);
// 定义 ref 来引用组件
const leftTree = ref<any>(null);
const departmentInfo = ref<any[]>([]);
//选择公司
const xzgs = ref('请选择公司');
//公司id
const xzgsid = ref();
//国资委id
const sum = ref<any>();
const Gzwid = ref('');
//职数
const position = ref('');
//领导班子数据
const Leadinggroupcode = ref('');
const Leadinggroup = ref('');
const Leadinggroupnumber = ref('');
//内设机构数据
const Internalmechanismcode = ref('');
const Internalmechanism = ref('');
const Internalmechanismnumber = ref('');
//左边树数据选择逻辑
async function onTreeSelect(data) {
  console.log('左侧树数据', data)
  //置空基本数据
  departmentInfo.value = []
  //赋值给公司机构信息
  Leadinggroupnumber.value = data.member
  position.value = data.position
  Internalmechanism.value = data.internalInstitutions
  Internalmechanismnumber.value = data.subsidiary
  xzgsid.value = data.id
  xzgs.value = data.departName
  //请求下一级数据 departName orgCategory
  const nextlevel = await queryDepartTreeSync({
    pid: data.id,
  });
  console.log('nextlevel', nextlevel)
  //双层遍历数据树 找到领导班子数据
  for (let i = 0; i < nextlevel.length; i++) {
    if (nextlevel[i].departName == '领导班子' && nextlevel[i].orgCategory != '1') {
      const nextlevels = await queryDepartTreeSync({
        pid: nextlevel[i].id,
      });
      if (nextlevel[i].chiefPositionDeputyPositionStr == '0-0') {
        nextlevel[i].chiefPositionDeputyPositionStr = '';
      }
      for (let i = 0; i < nextlevels.length; i++) {
        if (nextlevels[i].chiefPositionDeputyPositionStr == '0-0') {
          nextlevels[i].chiefPositionDeputyPositionStr = '';
        }
        departmentInfo.value.push(nextlevels[i])
      }
    } else {
      if (nextlevel[i].chiefPositionDeputyPositionStr == '0-0') {
        nextlevel[i].chiefPositionDeputyPositionStr = '';
      }
      if (nextlevel[i].orgCategory != '1') {
        departmentInfo.value.push(nextlevel[i])
      }
    }
  }

}
//保存数据
async function saveData() {
  console.log('数组数据', departmentInfo.value)
  const manpowerdata = ref<any[]>([]);; //数据数组
  sum.value = 0
  const frombuild = {
    id: xzgsid.value,
    member: Leadinggroupnumber.value,
    internalInstitutions: Internalmechanism.value,
    subsidiary: Internalmechanismnumber.value,
  }
  manpowerdata.value.push(frombuild)
  //塞入部门数据 position
  for (let i = 0; i < departmentInfo.value.length; i++) {
    // const re = splitPositions(departmentInfo.value[i].chiefPositionDeputyPositionStr);
    // departmentInfo.value[i].chiefPosition = re.chiefPosition;
    // departmentInfo.value[i].deputyPosition = re.deputyPosition;
    console.log(departmentInfo.value[i])
    departmentInfo.value[i].position= departmentInfo.value[i].position
    manpowerdata.value.push(departmentInfo.value[i]);
  }
  for (let i = 0; i < departmentInfo.value.length; i++) {
    sum.value = sum.value + departmentInfo.value[i].member
  }
  if (Leadinggroupnumber.value < sum.value) {
    message.error('公司总额' + Leadinggroupnumber.value + '小于各部门员额总数' + sum.value + '请仔细核对');
  } else {
    const Postmanagement = await saveOrUpdateDepart(manpowerdata.value);

    leftTree.value.loadRootTreeData();

  }
  console.log('frombuild', sum.value)

}
//部门分割数据
function splitPositions(str) {
  const parts = str.split('-');
  return {
    chiefPosition: parts[0],
    deputyPosition: parts[1],
  };
}
//领导班子分割数据
function splitPosition(str) {
  const parts = str.split('-');
  return {
    chiefPosition: parts[0],
    deputyPosition: parts[1],
    id: Leadinggroupcode.value,
    member: Leadinggroupnumber.value,
  };
}
</script>

<style lang="less">
@import './index.less';

.j-print-demo .ant-card-body {
  margin: 0;
  border: 0 solid black;
  min-width: 800px;
  color: #000000 !important;
}

.ldbz {
  display: flex;
  align-items: center;
}
</style>
